/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.SearchBook.Logic;

import Data.DAO.IBookStore;
import Data.DAO.IDataStore;
import data.DAO.ICategoryStore;
import data.pojo.Book;
import data.pojo.Category;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author LiuBole
 */
public class SearchLogic {

    private IDataStore bookDS = new IBookStore();
    private ICategoryStore categoryDS = new ICategoryStore();

    public Vector<List> searchKeywordsTableVector(String key, String keywords) {
        List arr = bookDS.searchObjectsByAttr(key, keywords);
        return new Vector<List>(toBookTableVector(arr));
    }

    public Vector<List> searchCategoryTableVector(String category) {
        List<Category> arr = categoryDS.searchObjectsByAttr("name", category);
        Vector<List> strList = new Vector();
        for (Category c : arr) {
            Vector<Vector<String>> list = toBookTableVector(c.getBookList());
            if (list != null) {
                strList.addAll(list);
            }
        }
        return strList;
    }
    //类别和关键字同时符合

    public Vector<List> searchAnd(String category, String key, String keywords) {
        List<Category> cList = categoryDS.searchObjectsByAttr("name", category);
        Vector<Book> str = new Vector<Book>();
        for (Category c : cList) {
            List<Book> bs = c.getBookList();
            if (bs != null) {
                str.addAll(bs);
            }
        }
        List<Book> reList = new ArrayList();
        List<Book> bList = bookDS.searchObjectsByAttr(key, keywords);
        for (Book b : bList) {
            String isbn = b.getISBN();
            for (Book s : str) {
                if (isbn.equals(s.getISBN())) {
                    reList.add(b);
                    break;
                }
            }

        }

        return new Vector<List>(toBookTableVector(reList));
    }

//    类别和关键字符合其一
    public Vector<List> searchOr(String keywords) {
        List<Category> cList = categoryDS.searchObjectsByAttr("name", keywords);
        Vector<Book> str = new Vector<Book>();
        for (Category c : cList) {
            List<Book> bs = c.getBookList();
            str = addInWithNoSame(str, bs);
        }
        
        String[] items = {"ISBN", "number", "title", "author", "price", "publisher"};
        for (int i = 0; i < items.length; i++) {
            List<Book> bList = bookDS.searchObjectsByAttr(items[i], keywords);
            str = addInWithNoSame(str, bList);
        }

        return new Vector<List>(toBookTableVector(str));
    }
    
    private Vector<Book> addInWithNoSame(Vector<Book> str, List<Book> bList){
        List<Book> tempList = new ArrayList();
        for (Book s : str) {
            if(bList==null){
                continue;
            }
            for (Book b : bList) {
                if (s.getISBN().equals(b.getISBN())) {
                    tempList.add(b);
                    break;
                }//end fi
            }//end for
        }//end for
        if (bList != null) {
            str.addAll(bList);
        }
        for (Book tb : tempList) {
            str.remove(tb);
        }
        return str;
    }

    private Vector<Vector<String>> toBookTableVector(List<Book> bookList){
        try {
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (Book book : bookList) {
                if(book==null){
                    System.out.println("D:NULL");
                }
                reList.add((Vector<String>) toBookStringArray(book));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    private List toBookStringArray(Book book){
        Vector<String> list = new Vector<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        list.add(book.getISBN());
        list.add(book.getTitle());
        list.add(book.getAuthor());
        list.add(String.valueOf(book.getPrice()));   
        list.add(book.getPublisher());
        Date date = book.getPublicationDate();
        if(date != null){
            list.add(sdf.format(book.getPublicationDate()));
        } else {
            list.add("");
        }
        return list;
    } 
    
    public List<String> getBookCategory() {
       List<String> cList = new ArrayList<String>();
       List<Category> list = categoryDS.searchObjectsByAttr("name", ".*"+""+".*");
       for(Category c : list){
           cList.add(c.getName());
       }
       return cList;
    }

}
